﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace com.lover.astd.game.ui
{
    public partial class TrainHeroCalc : Form
    {
        private int _curLevel = 1;
        private int _targetLevel = 1;
        private int _curReTrainLevel = 1;
        private int _starPerReTrain = 6;
        private int _expPerTufei = 0;
        private int _trainLevel = 0;
        private bool _useReTrain = false;
        private bool _useTufei = false;
        private double[] _trainLevelFactor = new double[] { 1.0, 1.5, 2.0, 2.5, 3.0, 4.0};
        private bool _trainer1 = false;
        private bool _trainer2 = false;
        private bool _trainer3 = false;
        private bool _trainer4 = false;
        public TrainHeroCalc()
        {
            InitializeComponent();
            combo_trainLevel.SelectedIndex = 0;
        }

        private void btn_calc_Click(object sender, EventArgs e)
        {
            _curLevel = (int)num_curLevel.Value;
            _curReTrainLevel = (int)num_curReTrainLevel.Value;
            _targetLevel = (int)num_targetLevel.Value;
            _starPerReTrain = (int)num_starPerReTrain.Value;
            _expPerTufei = (int)num_expPerTufei.Value;
            _trainLevel = combo_trainLevel.SelectedIndex;
            _useReTrain = chk_reTrain.Checked;
            _useTufei = chk_tufei.Checked;
            _trainer1 = chk_trainer1.Checked;
            _trainer2 = chk_trainer2.Checked;
            _trainer3 = chk_trainer3.Checked;
            _trainer4 = chk_trainer4.Checked;

            long exptotal = 0;
            if (!_useReTrain)
            {
                exptotal = getTotalExpToLevel(_curLevel, _targetLevel);
            }
            else
            {
                exptotal = getTotalExpToStar(_curLevel, _curReTrainLevel, _targetLevel, _starPerReTrain);
            }
            txt_result.Text = getTrainInfo(exptotal);
        }
        private string getTrainInfo(long exp)
        {
            long tufei_count = exp / _expPerTufei + 1;
            long tufei_count_true = 0; // 真实的突飞次数, 去掉训练位自身相当于的突飞次数
            double hours = 0;
            if (_useTufei)
            {
                // 突飞是每66分钟突飞11次, 相当于每小时突飞10
                // 郑玄+25%, 黄承彦大概是60%概率2倍暴击, 左慈是暴击时变成4倍, 甄宓是20%连续突飞2次
                double tufei_factor = 1.0;
                if (_trainer1) tufei_factor = tufei_factor * 1.25;
                if (_trainer2)
                {
                    if (_trainer3) tufei_factor = tufei_factor * 0.6 * 4;
                    else tufei_factor = tufei_factor * 0.6 * 2;
                }
                if (_trainer4) tufei_factor = tufei_factor * 1.2;

                hours = tufei_count / (_trainLevelFactor[_trainLevel] + tufei_factor * 10);
                tufei_count_true = tufei_count - (long)(hours * _trainLevelFactor[_trainLevel]);
            }
            else
            {
                hours = tufei_count / _trainLevelFactor[_trainLevel];
            }
            return String.Format("总计需要经验值:\t{0:d}\r\n突飞次数:\t{1:d}\r\n需要小时数:\t{2:f2} ({3:f2}天)\r\n", exp, tufei_count_true, hours, hours / 24);
        }
        private long getTotalExpToStar(int curLevel, int curRetrainLevel, int targetLevel, int starPerRetrain)
        {
            long exptotal = 0;
            // 首先看到当前转生需要多少经验
            exptotal = getTotalExpToLevel(curLevel, curRetrainLevel);
            while (curRetrainLevel + starPerRetrain <= targetLevel + 1)
            {
                curRetrainLevel += starPerRetrain;
                exptotal += getTotalExpToLevel(1, curRetrainLevel);
            }

            return exptotal;
        }
        private long getTotalExpToLevel(int fromLevel, int toLevel)
        {
            long exp = 0;
            for (int i = fromLevel; i < toLevel; i++)
            {
                exp += getLevelExp(i);
            }
            return exp;
        }
        private long getLevelExp(int level)
        {
            long exp = 0;
            if (level == 1) exp = 45;
            else if (level == 2) exp = 180;
            else if (level == 3) exp = 450;
            else if (level == 4) exp = 720;
            else if (level == 5) exp = 900;
            else if (level > 5 && level < 16) exp = 900 + (level - 6) * 900;
            else if (level >= 16 && level < 57) exp = 10800 + (level - 16) * 1800;
            else if (level == 57) exp = 87300;
            else if (level >= 58 && level < 79) exp = 90000 + (level - 58) * 9000;
            else if (level >= 79 && level < 89) exp = 297000 + (level - 79) * 27000;
            else if (level == 89) exp = 576000;
            else if (level == 90) exp = 594000;
            else if (level == 91) exp = 621000;
            else if (level == 92) exp = 648000;
            else if (level >= 93) exp = 684000 + (level - 93) * 36000;
            return exp;
        }
    }
}
